package com.tianji.aopdemo.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author Yandel
 * @date 2025/8/18 下午7:21
 * @description: Aop
 */
@Component
@Aspect //表示切面
public class AopConfig {

    //切点 表示要切入的位置  service  ..  这里的点点表示service下的包
//    @Pointcut("execution(public * com.tianji.aopdemo.service..*.get(..))")
//    private void pt(){}

//    @Pointcut("@annotation(com.tianji.aopdemo.config.PrintTime)")
//    private void pt(){}


//    @Before() //前置通知
//    @After() //后置通知
//    @AfterReturning() //返回通知
//    @AfterReturning() //异常通知
    //环绕通知
/*    @Around("pt()")
    public  Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("开始时间："+new Date( ));
        Object[] args = pjp.getArgs();
        Object ret = pjp.proceed(args);//执行目标方法
        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null){
            PrintTime methodAnnotation = method.getAnnotation(PrintTime.class);
            if (methodAnnotation!=null) {
                System.out.println(methodAnnotation.title());
            }
        }

        System.out.println("结束时间："+new Date());
        return ret;
    } */

    @Around("@annotation(PrintTime)")//切点切入到加了自定义注解处
    public  Object around(ProceedingJoinPoint pjp, PrintTime PrintTime) throws Throwable {
        System.out.println("开始时间："+new Date( ));
        Object[] args = pjp.getArgs();
        Object ret = pjp.proceed(args);//执行目标方法
        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null){
            PrintTime methodAnnotation = method.getAnnotation(PrintTime.class);
            if (methodAnnotation!=null) {
                System.out.println(methodAnnotation.title());
            }
        }

        System.out.println("结束时间："+new Date());
        return ret;
    }

}
